package com.jsmile.cloud.tradecenter.order.feign;

import javax.validation.Valid;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jsmile.cloud.tradecenter.api.req.ReqOrderDetail;
import com.jsmile.cloud.tradecenter.api.req.ReqOrderQuery;
import com.jsmile.cloud.tradecenter.api.res.ResOrder;
import com.jsmile.cloud.tradecenter.order.OrderApplicationService;
import com.jsmile.mail.security.controller.AuthcController;
import com.jsmile.mail.security.handler.JsmileContext;
import com.jsmile.mall.api.JSmileResult;
import com.jsmile.mall.api.annotation.CurrentContext;
import com.jsmile.mall.api.constants.JSmileConstant;
import com.jsmile.mall.api.enums.LogTypeEnum;
import com.jsmile.mall.api.to.JsmilePage;
import com.jsmile.mall.log.annotation.JsmileLog;

import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@RestController
@Slf4j
@AllArgsConstructor
@Api(tags = "订单管理")
public class OrderSearchServiceApiImpl extends AuthcController {

    private final OrderApplicationService orderApplicationService;

    @RequestMapping(value = "/feign/order/search/findAll", method = RequestMethod.POST)
    @JsmileLog(title = "订单中心->订单查询", type = LogTypeEnum.QUERY, systemId = JSmileConstant.SystemId.TRADE_CENTER)
    public JSmileResult<JsmilePage<ResOrder>> findAll(@RequestBody @Valid ReqOrderQuery reqOrderQuery, @CurrentContext JsmileContext jsmileContext) {
        reqOrderQuery.setCurTenantId(jsmileContext.getTenantId());
        IPage<ResOrder> pages = orderApplicationService.findAll(reqOrderQuery);
        return JSmileResult.success(new JsmilePage(reqOrderQuery.getPageIndex(), reqOrderQuery.getPageSize(), pages.getTotal(), pages.getRecords()));
    }

    @RequestMapping(value = "/feign/order/search/findByOrderNo", method = RequestMethod.POST)
    @JsmileLog(title = "订单中心->订单详情查询(不含商品等信息)", type = LogTypeEnum.QUERY, systemId = JSmileConstant.SystemId.TRADE_CENTER)
    public JSmileResult<JsmilePage<ResOrder>> findByOrderNo(@RequestBody @Valid ReqOrderDetail reqOrderDetail, @CurrentContext JsmileContext jsmileContext) {
        reqOrderDetail.setCurTenantId(jsmileContext.getTenantId());
        return JSmileResult.success(orderApplicationService.findByOrderNo(reqOrderDetail));
    }

    @RequestMapping(value = "/feign/order/search/findBySimpleOrderNo", method = RequestMethod.POST)
    @JsmileLog(title = "订单中心->订单详情查询(含商品等信息)", type = LogTypeEnum.QUERY, systemId = JSmileConstant.SystemId.TRADE_CENTER)
    public JSmileResult<JsmilePage<ResOrder>> findBySimpleOrderNo(@RequestBody @Valid ReqOrderDetail reqOrderDetail, @CurrentContext JsmileContext jsmileContext) {
        reqOrderDetail.setCurTenantId(jsmileContext.getTenantId());
        return JSmileResult.success(orderApplicationService.findBySimpleOrderNo(reqOrderDetail));
    }
}
